using System;
using UnityEngine;
using System.Runtime.InteropServices;



public class MapNetHandler : Singleton< MapNetHandler > 
{
	
	void Awake ()
	{
		if ( mInstance == null )
		{
			mInstance = this;
			initHandler();
		}
		
	}
	
	public void initHandler()
	{
		GameSocketManager.instance.regeditMsg( (int)GameNetMessage.MsgType._MSG_RECV_ENTERMAP , onRecvEnterMap , typeof( GameNetMessage.RECV_MSG_ENTERMAP ) );
		GameSocketManager.instance.regeditMsg( (int)GameNetMessage.MsgType._MSG_RECV_EXITMAP , onRecvExitMap , typeof( GameNetMessage.RECV_MSG_EXITMAP ) );

		GameSocketManager.instance.regeditMsg( (int)GameNetMessage.MsgType._MSG_RECV_MAPINFO , onRecvMapInfo , typeof( GameNetMessage.RECV_MSG_MAPINFO ) );
		GameSocketManager.instance.regeditMsg( (int)GameNetMessage.MsgType._MSG_RECV_NPCENTERMAP , onRecvNpcEnterMap , typeof( GameNetMessage.RECV_MSG_NPCENTERMAP ) );



		GameSocketManager.instance.regeditMsg( (int)GameNetMessage.MsgType._MSG_RECV_MOVE , onRecvMove , typeof( GameNetMessage.SEND_RECV_MSG_MOVE ) );
		GameSocketManager.instance.regeditMsg( (int)GameNetMessage.MsgType._MSG_SEND_MOVE_ENTER , onRecvMoveEnter , typeof( GameNetMessage.SEND_RECV_MSG_MOVE_ENTER ) );
		GameSocketManager.instance.regeditMsg( (int)GameNetMessage.MsgType._MSG_SEND_MOVE_EXIT , onRecvMoveExit , typeof( GameNetMessage.SEND_RECV_MSG_MOVE_EXIT ) );

		GameSocketManager.instance.regeditMsg( (int)GameNetMessage.MsgType._MSG_RECV_PLAYERENTERMAP , onRecvPlayerEnterMap , typeof( GameNetMessage.RECV_MSG_PLAYERENTERMAP ) );

	}

	public MapNetHandler ()
	{
	}


	public unsafe void onRecvPlayerEnterMap( GameNetMessage.NetMsgInterface head )
	{
		GameNetMessage.RECV_MSG_PLAYERENTERMAP msg = ( GameNetMessage.RECV_MSG_PLAYERENTERMAP )head;

		GameObject obj = GamePlayerData.instance.createPlayer( msg.Data.GUID );
		SAPlayer player = obj.GetComponent< SAPlayer >();
		
		player.creatuerData.setPlayerData( msg.Data );
		
		player.initPlayer();
		player.updateAction();
		player.setPos( msg.Data.Pos.X , msg.Data.Pos.Y );

		if ( msg.TeamCount > 0 )
		{
			int index = 0;
			byte[] buff = GameSocketManager.instance.getIbuffer( out index );

			SAPlayerData[] data = GameDefine.bytesToStructArray< SAPlayerData >( buff , index + 8 + Marshal.SizeOf( typeof( SAPlayerData ) ) , typeof( SAPlayerData ) , msg.TeamCount );
		}
	}

	public void onRecvExitMap( GameNetMessage.NetMsgInterface head )
	{
		GameNetMessage.RECV_MSG_EXITMAP msg = ( GameNetMessage.RECV_MSG_EXITMAP )head;

		GamePlayerData.instance.removePlayer( msg.GUID );
	}

	public void onRecvMoveExit( GameNetMessage.NetMsgInterface head  )
	{
		GameNetMessage.SEND_RECV_MSG_MOVE_EXIT msg = ( GameNetMessage.SEND_RECV_MSG_MOVE_EXIT )head;

		GamePlayerData.instance.enabledPlayer( msg.GUID , false );
	}

	public void onRecvMoveEnter( GameNetMessage.NetMsgInterface head  )
	{
		GameNetMessage.SEND_RECV_MSG_MOVE_ENTER msg = ( GameNetMessage.SEND_RECV_MSG_MOVE_ENTER )head;
		
		GamePlayerData.instance.enabledPlayer( msg.GUID , true , msg.X , msg.Y );
	}

	public unsafe void onRecvMove( GameNetMessage.NetMsgInterface head )
	{
		GameNetMessage.SEND_RECV_MSG_MOVE msg = ( GameNetMessage.SEND_RECV_MSG_MOVE )head;

		GameObject obj = GamePlayerData.instance.getPlayer( msg.GUID );

		if ( obj == null )
		{
			return;
		}

		if ( SAMapManager.instance.mapID != msg.MapID ) 
		{
			return;
		}

		int[] path = new int[ msg.Count ];
		for ( int i = 0; i < msg.Count ; i++ )
		{
			path[ i ] = msg.Path[ i ];
		}

		SAPlayer player = obj.GetComponent< SAPlayer >();
		player.startMove( path );
	}

	public void onRecvAction( GameNetMessage.NetMsgInterface head )
	{
		
	}

	public void onRecvDirection( GameNetMessage.NetMsgInterface head )
	{
		
	}



	public unsafe void onRecvMapInfo( GameNetMessage.NetMsgInterface head )
	{
		GameNetMessage.RECV_MSG_MAPINFO msg = ( GameNetMessage.RECV_MSG_MAPINFO )head;

		int index = 0;
		byte[] buff = GameSocketManager.instance.getIbuffer( out index );

		if ( msg.Count > 0 )
		{
			SAPlayerData[] data = GameDefine.bytesToStructArray< SAPlayerData >( buff , index + 8 , typeof( SAPlayerData ) , msg.Count );

			for ( int i = 0 ; i < msg.Count ; i++ )
			{
				GameObject obj = GamePlayerData.instance.createPlayer( data[ i ].GUID );
				SAPlayer player = obj.GetComponent< SAPlayer >();
				
				player.creatuerData.setPlayerData( data[ i ] );

				player.initPlayer();
				player.updateAction();
				player.setPos( data[ i ].Pos.X , data[ i ].Pos.Y );
			}
		}

	}

	public void onRecvNpcEnterMap( GameNetMessage.NetMsgInterface head )
	{
		//SANpcData
	}

	public void onRecvEnterMap( GameNetMessage.NetMsgInterface head )
	{
		GameNetMessage.RECV_MSG_ENTERMAP msg = ( GameNetMessage.RECV_MSG_ENTERMAP )head;

		GamePlayerData.instance.Pos = msg.Pos;

		//Debug.Log( "onRecvEnterMap" + msg.Pos.MapID + " x:" + msg.Pos.X + " y:" + msg.Pos.Y );

		if ( SAMapManager.instance && SAMapManager.instance.mainPlayer )
		{
			SAMapManager.instance.mainPlayer.endMoving();
		}
		
		int level = Application.loadedLevel; 

		if ( level == 1 )
		{
			GamePlayerData.instance.clearData();
			SAMapManager.instance.loadMapAsync();
		}
		else
		{
			LoginLoadingUIHandler.instance.Show();
			LoginLoadingUIHandler.instance.loadScene( 1 );
		}


	}


}

